#!/bin/bash

# 设置基本路径（你需要根据实际路径调整）
WORKPUBLISH_DIR="/data/apps/minfs/workpublish"
BASE_DIR="/data/apps/minfs"

# 清理旧的发布目录
rm -rf $WORKPUBLISH_DIR

# 创建发布目录结构
mkdir -p $WORKPUBLISH_DIR/bin
mkdir -p $WORKPUBLISH_DIR/dataServer
mkdir -p $WORKPUBLISH_DIR/metaServer
mkdir -p $WORKPUBLISH_DIR/easyClient

echo "Building dataServer..."
cd $BASE_DIR/dataServer
mvn clean package -Dmaven.test.skip=true
cp target/dataServer-1.0.jar $WORKPUBLISH_DIR/dataServer/

echo "Building metaServer..."
cd $BASE_DIR/metaServer
mvn clean package -Dmaven.test.skip=true
cp target/metaServer-1.0.jar $WORKPUBLISH_DIR/metaServer/

echo "Building easyClient..."
cd $BASE_DIR/easyClient
mvn clean package -Dmaven.test.skip=true
cp target/easyClient-1.0.jar $WORKPUBLISH_DIR/easyClient/

# 回到工作目录
cd $WORKPUBLISH_DIR

# 创建启动脚本
echo "Creating start.sh..."
cat <<EOF >$WORKPUBLISH_DIR/bin/start.sh
#!/bin/bash

# 设置实际路径（替换下面的路径为你的实际路径）
WORKPUBLISH_DIR="/data/apps/minfs/workpublish"
BASE_DIR="/data/apps/minfs"


# 创建日志目录
mkdir -p $WORKPUBLISH_DIR/dataServer
mkdir -p $WORKPUBLISH_DIR/metaServer

# 启动 dataServer 实例
echo "Starting dataServer instances..."
nohup java -jar $WORKPUBLISH_DIR/dataServer/dataServer-1.0.jar --spring.config.location=$BASE_DIR/dataServer/src/main/resources/application.yml > $WORKPUBLISH_DIR/dataServer/dataServer-9001.log 2>&1 &
sleep 2
nohup java -jar $WORKPUBLISH_DIR/dataServer/dataServer-1.0.jar --spring.config.location=$BASE_DIR/dataServer/src/main/resources/application-data-9002.yml > $WORKPUBLISH_DIR/dataServer/dataServer-9002.log 2>&1 &
sleep 2
nohup java -jar $WORKPUBLISH_DIR/dataServer/dataServer-1.0.jar --spring.config.location=$BASE_DIR/dataServer/src/main/resources/application-data-9003.yml > $WORKPUBLISH_DIR/dataServer/dataServer-9003.log 2>&1 &
sleep 2
nohup java -jar $WORKPUBLISH_DIR/dataServer/dataServer-1.0.jar --spring.config.location=$BASE_DIR/dataServer/src/main/resources/application-data-9004.yml > $WORKPUBLISH_DIR/dataServer/dataServer-9004.log 2>&1 &

sleep 2

# 启动 metaServer 实例
echo "Starting metaServer instances..."
nohup java -jar $WORKPUBLISH_DIR/metaServer/metaServer-1.0.jar --spring.config.location=$BASE_DIR/metaServer/src/main/resources/application.yml --spring.profiles.active=master > $WORKPUBLISH_DIR/metaServer/metaServer-master.log 2>&1 &
sleep 3
nohup java -jar $WORKPUBLISH_DIR/metaServer/metaServer-1.0.jar --spring.config.location=$BASE_DIR/metaServer/src/main/resources/application.yml  --spring.profiles.active=slave> $WORKPUBLISH_DIR/metaServer/metaServer-slave.log 2>&1 &

echo "All services started."
EOF

# 赋予启动脚本执行权限
chmod +x $WORKPUBLISH_DIR/bin/start.sh

echo "Build and packaging complete."
